package com.htfyun.eink.pw.draw.pen;

import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;

import androidx.annotation.NonNull;

import com.htfyun.eink.pw.configs.ConfigPen;
import com.htfyun.eink.pw.draw.IPWDrawPath;
import com.htfyun.eink.pw.draw.pointer.PWPointer;

public class PenRandomEraser extends PenRandomBase implements IPenEraser, IPenShape {

    public static final String PEN_ID = PenRandomEraser.class.getSimpleName();
    protected final Path floatPath = new Path();
    protected final Paint floatPaint = new Paint();

    private final static int LINE_SIZE = 10;

    private final Xfermode CLEAR_MODE = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
    @NonNull
    @Override
    public String getPenId() {
        return PEN_ID;
    }

    @Override
    public void configPaint(@NonNull Paint paintRandom, @NonNull PWPointer pointer) {
        paintRandom.setXfermode(CLEAR_MODE);
    }

    @Override
    public boolean cacheStrokeWidth() {
        return false;
    }

    @Override
    public PenDrawPointerResultEnums pointerDown(@NonNull IPWDrawPath draw, @NonNull PWPointer pointer) {
        draw.getPaintConfig().configPaint(floatPaint);
        floatPaint.setStrokeWidth(ConfigPen.WRITER_STROKE_WIDTH_DEFAULT);
        floatPaint.setColor(ConfigPen.WRITER_COLOR_DEFAULT);
        return super.pointerDown(draw, pointer);
    }

    @Override
    public PenDrawPointerResultEnums pointerMove(@NonNull IPWDrawPath draw, @NonNull PWPointer pointer) {
        super.pointerMove(draw, pointer);

        floatPath.reset();
        floatPath.moveTo(pointer.x - LINE_SIZE, pointer.y);
        floatPath.lineTo(pointer.x + LINE_SIZE, pointer.y);

        floatPath.moveTo(pointer.x, pointer.y - LINE_SIZE);
        floatPath.lineTo(pointer.x, pointer.y + LINE_SIZE);
        return PenDrawPointerResultEnums.INVALIDATE_VIEW_ALL;
    }

    @Override
    public PenDrawPointerResultEnums pointerUp(@NonNull IPWDrawPath draw, @NonNull PWPointer pointer) {
        super.pointerUp(draw, pointer);
        floatPath.reset();
        return PenDrawPointerResultEnums.INVALIDATE_VIEW_ALL;
    }

    @NonNull
    @Override
    public Path getFloatPath() {
        return floatPath;
    }

    @NonNull
    @Override
    public Paint getFloatPaint() {
        return floatPaint;
    }
}
